<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="playlist_name" label="列表名称" required>
        <uni-easyinput placeholder="列表名称" v-model="formData.playlist_name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="playlist_name_en" label="列表英文名称">
        <uni-easyinput placeholder="列表英文名称" v-model="formData.playlist_name_en"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="playlist_type" label="列表类型" required>
        <uni-data-checkbox v-model="formData.playlist_type" :localdata="formOptions.playlist_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="cover_image" label="封面图片">
        <uni-easyinput placeholder="封面图片URL" v-model="formData.cover_image"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="background_color" label="背景颜色">
        <uni-easyinput placeholder="背景颜色代码，例如 #FF5733" v-model="formData.background_color"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="patterns" label="图案列表">
        <uni-data-checkbox :multiple="true" v-model="formData.patterns"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="is_active" label="是否激活">
        <switch @change="binddata('is_active', $event.detail.value)" :checked="formData.is_active"></switch>
      </uni-forms-item>
      <uni-forms-item name="sort_order" label="显示顺序">
        <uni-easyinput placeholder="在首页中的显示顺序" type="number" v-model="formData.sort_order"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="description" label="描述">
        <uni-easyinput placeholder="列表描述" v-model="formData.description"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/sh_featured_playlist.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'sh_featured_playlist';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "playlist_name": "",
        "playlist_name_en": "",
        "playlist_type": "",
        "cover_image": "",
        "background_color": "",
        "patterns": [],
        "is_active": null,
        "sort_order": null,
        "description": ""
      }
      return {
        formData,
        formOptions: {
          "playlist_type_localdata": [
            {
              "value": "nature",
              "text": "nature"
            },
            {
              "value": "art",
              "text": "art"
            },
            {
              "value": "abstract",
              "text": "abstract"
            },
            {
              "value": "geometry",
              "text": "geometry"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
